/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jinyao.exploit.modules.gen.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinyao.exploit.common.base.User;
import com.jinyao.exploit.common.base.entity.BaseResult;
import com.jinyao.exploit.common.base.web.BaseController;
import com.jinyao.exploit.common.utils.StringUtils;
import com.jinyao.exploit.common.utils.UserUtils;
import com.jinyao.exploit.modules.gen.entity.Table;
import com.jinyao.exploit.modules.gen.service.TableService;
import com.jinyao.exploit.modules.gen.util.GenUtils;
import com.jinyao.exploit.modules.sys.entity.Menu;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 自动生成表体Controller
 *
 * @author tm
 * @version 2019/06/26 15:22
 */
@RestController
@RequestMapping(value = "${baseAdminPath}/${apiPath}/gen/table")
public class TableApiController extends BaseController {

	@Resource(name = "tableServiceImpl")
	private TableService tableService;
	
	@ModelAttribute
	public Table get(@RequestParam(required=false) long id) {
		if (id != 0){
			return tableService.getById(id);
		}else{
			return new Table();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public BaseResult list(Table table, HttpServletRequest request, HttpServletResponse response, Model model) {
		QueryWrapper<Table> wrapper = Wrappers.query();
		wrapper.eq("name", "11111");
        return BaseResult.ok(tableService.page(new Page<>(),wrapper));
	}

	@RequestMapping(value = "form")
	public String form(Table table, Model model) {
		// 获取物理表列表
		List<Table> tableList = tableService.findTableListFormDb(new Table());
		model.addAttribute("tableList", tableList);
		// 验证表是否存在
		if (table.getId() == 0 && !tableService.checkTableName(table.getName())){
			addMessage(model, "下一步失败！" + table.getName() + " 表已经添加！");
			table.setName("");
		}
		// 获取物理表字段
		else{
			table = tableService.getTableFormDb(table);
		}
		model.addAttribute("table", table);
		model.addAttribute("config", GenUtils.getConfig());
		return "modules/gen/tableForm";
	}

	@RequestMapping(value = "save")
	public String save(Table table, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, table)){
			return form(table, model);
		}
		// 验证表是否已经存在
		if (table.getId() == 0 && !tableService.checkTableName(table.getName())){
			addMessage(model, "保存失败！" + table.getName() + " 表已经存在！");
			table.setName("");
			return form(table, model);
		}
		tableService.saveOrUpdate(table);
		addMessage(redirectAttributes, "保存业务表'" + table.getName() + "'成功");
		return "redirect:" + adminPath + "/gen/table/?repage";
	}

	@RequestMapping(value = "delete")
	public String delete(Table table, RedirectAttributes redirectAttributes) {
		tableService.removeById(table.getId());
		addMessage(redirectAttributes, "删除业务表成功");
		return "redirect:" + adminPath + "/gen/table/?repage";
	}

}
